package com.wsd.web;

import com.wsd.common.Result;
import com.wsd.constant.ResultCode;
import com.wsd.domain.*;
import com.wsd.service.LoanInformationService;
import com.wsd.service.LoanLimitService;
import com.wsd.service.RepaymentPlanService;
import com.wsd.service.RepaymentRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
public class RepaymentController {

    @Autowired
    private RepaymentRecordService repaymentRecordService;

    @Autowired
    private LoanInformationService loanInformationService;

    @Autowired
    private LoanLimitService loanLimitService;

    @Autowired
    private RepaymentPlanService repaymentPlanService;

    /**
     * 根据客户编号查找还款信息
     *
     * @param customerId
     * @return
     */
    @RequestMapping("/repaymentRecord")
    @ResponseBody
    public Result selectRepaymentRecord(@RequestParam("customerId") String customerId) {
        Result result = new Result();
        if (StringUtils.isEmpty(customerId)) {
            result.setResultCode(ResultCode.PARAM_NOT_COMPLETE);
            return result;
        }
        List<RepaymentRecord> repaymentRecord = repaymentRecordService.selectRecordByCustomerId(customerId);
        result.setData(repaymentRecord);
        result.setResultCode(ResultCode.SUCCESS);
        return result;
    }

    /**
     * 根据客户编号查找贷款信息
     *
     * @param customerId
     * @return
     */
    @RequestMapping("/loan")
    @ResponseBody
    public Result selectLoan(@RequestParam("customerId") String customerId) {
        Result result = new Result();
        if (StringUtils.isEmpty(customerId)) {
            result.setResultCode(ResultCode.PARAM_NOT_COMPLETE);
            return result;
        }
        List<LoanInformation> loanInformation = loanInformationService.selectLoanByCustomerId(customerId);
        result.setData(loanInformation);
        result.setResultCode(ResultCode.SUCCESS);
        return result;
    }

    /**
     * 根据客户编号查找最高额度
     *
     * @param customerId
     * @return
     */
    @RequestMapping("/limit")
    @ResponseBody
    public Result selectLoanLimit(@RequestParam("customerId") String customerId) {
        Result result = new Result();
        if (StringUtils.isEmpty(customerId)) {
            result.setResultCode(ResultCode.PARAM_NOT_COMPLETE);
            return result;
        }
        LoanLimit loanLimit = loanLimitService.selectByCustomerId(customerId);
        result.setData(loanLimit);
        result.setResultCode(ResultCode.SUCCESS);
        return result;
    }

    /**
     * 根据订单编号查看贷款总详细
     *
     * @param loanId
     * @return
     */
    @RequestMapping("/details")
    @ResponseBody
    public Result selectLoanDetails(@RequestParam("loanId") String loanId) {
        Result result = new Result();
        if (StringUtils.isEmpty(loanId)) {
            result.setResultCode(ResultCode.PARAM_NOT_COMPLETE);
            return result;
        }
        long id = Long.parseLong(loanId);
        LoanDetails loanDetails = loanInformationService.selectLoanDetailsByLoanId(id);
        result.setData(loanDetails);
        result.setResultCode(ResultCode.SUCCESS);
        return result;
    }

    /**
     * 根据订单编号查看贷还款记录
     *
     * @param loanId
     * @return
     */
    @RequestMapping("/repay")
    @ResponseBody
    public Result selectRepayByLoanId(@RequestParam("loanId") String loanId) {
        Result result = new Result();
        if (StringUtils.isEmpty(loanId)) {
            result.setResultCode(ResultCode.PARAM_NOT_COMPLETE);
            return result;
        }
        List<RepaymentPlan> repaymentPlans = repaymentPlanService.selectRepaymentPlanByLoanId(Long.parseLong(loanId));
        result.setData(repaymentPlans);
        result.setResultCode(ResultCode.SUCCESS);
        return result;
    }

    /**
     * 根据订单编号查询某次贷款的所有还款记录详细
     *
     * @param loanId
     * @return
     */
    @RequestMapping("repayDetails")
    @ResponseBody
    public Result selectRepaymentPlanByLoanIdAndStatus(@RequestParam("loanId") String loanId) {
        Result result = new Result();
        if (StringUtils.isEmpty(loanId)) {
            result.setResultCode(ResultCode.PARAM_NOT_COMPLETE);
            return result;
        }
        List<RepaymentPlan> repaymentPlans = repaymentPlanService.selectRepaymentPlanByLoanIdAndStatus(loanId);
        result.setData(repaymentPlans);
        result.setResultCode(ResultCode.SUCCESS);
        return result;
    }
}
